@extends('layouts.main')

@section('title', '开票指标_重点产品')

@section('content')
<h3>@yield('title')</h3>
<div class="btn-group" role="group">
  	<a href="{{ action('BusinessData\InvoiceTargetKeyProductController@create') }}" class="btn btn-primary">添加指标数据</a>
</div>

@if(session('success'))
<div class="alert alert-success" role="alert">{{ session('success') }}</div>
@endif

<div class="clearfix"></div>
<hr>

<form class="form-inline pull-right" method="get" action="{{ action('BusinessData\InvoiceTargetKeyProductController@index') }}">
    
    <div class="form-group">
        <label for="year">年份</label>
        <select class="form-control" id="year" name="year">
            <option>请选择年份</option>
            @for($i = 2020; $i <= 2030; $i++)
            <option value="{{ $i }}" @if(request()->year == $i) selected @endif>{{ $i }}</option>
            @endfor
        </select>
    </div>

    <div class="form-group">
        <label for="sales_region_code">销售大区</label>
        <select class="form-control" id="sales_region_code" name="sales_region_code">
            <option>请选择销售大区</option>
            @foreach($sales_regions as $sales_region)
            <option value="{{ $sales_region->code }}" @if(request()->sales_region_code == $sales_region->code) selected @endif>{{ $sales_region->code.' --- '.$sales_region->code_desc }}</option>
            @endforeach
        </select>
    </div>

    <div class="form-group">
        <label for="category_key_product_id">重点产品分类</label>
        <select class="form-control" id="category_key_product_id" name="category_key_product_id">
            <option>请选择重点产品分类</option>
            @foreach($category_key_products as $value)
            <option value="{{ $value->id }}" @if(request()->category_key_product_id == $value->id) selected @endif>{{ $value->name }}</option>
            @endforeach
        </select>
    </div>

    <button type="submit" name="search" value="search" class="btn btn-primary">查询</button>

</form>

<div class="clearfix"></div>
<hr>

<table class="table table-bordered table-striped table-hover">
    <tr>
		<th>id</th>
		<th>年度</th>
		<th>大区</th>
		<th>重点产品分类</th>
		<th>指标(台数)</th>
		<th>操作</th>
    </tr>
    @forelse($models as $model)
	<tr>
		<td>{{ $model->id }}</td>
		<td>{{ $model->year }}</td>
		<td>{{ $model->sales_region->code_desc }}</td>
		<td>{{ $model->category_key_product->name }}</td>
		<td>{{ number_format($model->quantity, 2) }}</td>
		<td>
			<a href="{{ action('BusinessData\InvoiceTargetKeyProductController@edit', $model) }}" class="btn btn-warning btn-sm">编辑</a>
			<button name="delete" value="{{ $model->id }}" class="btn btn-danger btn-sm">删除</button>
		</td>
	</tr>
    @empty
  	<tr><td>暂无数据</td></tr>
    @endforelse
    <tr>
        <th>合计</th>
        <th></th>
        <th></th>
        <th></th>
        <th>{{ number_format($total_quantity, 2) }}</th>
        <th></th>
    </tr>
</table>
{{ $models->appends([
    'year' => request()->year,
    'sales_region_code' => request()->sales_region_code,
    'category_key_product_id' => request()->category_key_product_id,
    ])->links() }}
<script>
      $(document).ready(function(){
        $("button[name=delete]").click(function(){
            var that = $(this);
            var id = that.attr("value");
            $.post("/business_data/invoice_target_key_product/" + id, {
                '_method': 'DELETE',
                '_token': '{{ csrf_token() }}',
            }, function(data){
                switch(data.status){
                    case 0:
                        that.parent().parent('tr').remove();
                        alert("删除信息成功！");
                        break;
                }
            });
        });
    });
</script>
@endsection